package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.LogUtils
import com.github.gzuliyujiang.wheelview.contract.OnWheelChangedListener
import com.github.gzuliyujiang.wheelview.widget.WheelView
import com.lv.common.im.OnPositionMoreCancle
import com.lv.service.R
import com.lv.service.room.address.ReceiptAddressItem
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class CategoryBottom4PopupView(
    private val mContext: Context,
    private val mTitle: String = "",
    private val mAllBeans: List<ReceiptAddressItem>?,
    private var mWheelPosition1: Int = 0,
    private var mWheelPosition2: Int = 0,
    private var mWheelPosition3: Int = 0,
    private var mWheelPosition4: Int = 0,
    private var mWheelPos: Int? = -1,//显示第几个 -1 全部显示
    private val positionListener: OnPositionMoreCancle<String>? = null
) :
    BottomPopupView(mContext) {
    private var tvTitle: TextView? = null
    private var tvSure: TextView? = null
    private var wheelLinkagePorvince: WheelView? = null
    private var wheelLinkageCity: WheelView? = null
    private var wheelLinkageArea: WheelView? = null
    private var wheelLinkageTown: WheelView? = null
    private var viewTop: View? = null

    override fun getImplLayoutId(): Int {
        return R.layout.category_bottom4_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewTop = findViewById<View>(R.id.view_top)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        tvSure = findViewById<TextView>(R.id.tv_sure)
        wheelLinkagePorvince = findViewById<WheelView>(R.id.wheel_linkage_province)
        wheelLinkageCity = findViewById<WheelView>(R.id.wheel_linkage_city)
        wheelLinkageArea = findViewById<WheelView>(R.id.wheel_linkage_area)
        wheelLinkageTown = findViewById<WheelView>(R.id.wheel_linkage_town)
        initView()
        initEvnet()
    }

    private fun initView() {
        tvTitle?.text = mTitle

        when (mWheelPos) {
            0 -> {
                wheelLinkagePorvince?.visibility = View.VISIBLE
                wheelLinkageCity?.visibility = View.GONE
                wheelLinkageArea?.visibility = View.GONE
                wheelLinkageTown?.visibility = View.GONE
                wheelLinkagePorvince?.data = mAllBeans
                initWheel()

            }
            1 -> {
                wheelLinkagePorvince?.visibility = View.VISIBLE
                wheelLinkageCity?.visibility = View.VISIBLE
                wheelLinkageArea?.visibility = View.GONE
                wheelLinkageTown?.visibility = View.GONE
            }
            2 -> {
                wheelLinkagePorvince?.visibility = View.VISIBLE
                wheelLinkageCity?.visibility = View.VISIBLE
                wheelLinkageArea?.visibility = View.VISIBLE
                wheelLinkageTown?.visibility = View.GONE
            }
            else -> {
                wheelLinkagePorvince?.visibility = View.VISIBLE
                wheelLinkageCity?.visibility = View.VISIBLE
                wheelLinkageArea?.visibility = View.VISIBLE
                wheelLinkageTown?.visibility = View.VISIBLE
                initWheel()

            }

        }


    }

    private fun initWheel() {

        wheelLinkagePorvince?.data = mAllBeans
        wheelLinkagePorvince?.setDefaultPosition(mWheelPosition1)
        wheelLinkageCity?.data = mAllBeans?.get(mWheelPosition1)?.telSecondCategories
        wheelLinkageCity?.setDefaultPosition(mWheelPosition2)
        wheelLinkageArea?.data =
            mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories
        wheelLinkageArea?.setDefaultPosition(mWheelPosition3)
        wheelLinkageTown?.data =
            mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(mWheelPosition3)?.telFourCategories
        wheelLinkageTown?.setDefaultPosition(mWheelPosition4)
    }

    private fun initEvnet() {

        viewTop?.setOnClickListener {
            dismiss()
        }
        findViewById<ImageView>(R.id.iv_close).setOnClickListener {

            dismiss()
        }
        tvSure?.setOnClickListener {
            positionListener?.onConfirmClick(
                mWheelPosition1,
                mWheelPosition2,
                mWheelPosition3,
                mWheelPosition4,
                data = null
            )
            dismiss()
        }

        wheelLinkagePorvince?.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onWheelScrolled(view: WheelView?, offset: Int) {
                LogUtils.e("onWheelScrolled")
            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                LogUtils.e("onWheelScrolled==")

                mWheelPosition1 = position
                mWheelPosition2 = 0
                mWheelPosition3 = 0
                mWheelPosition4 = 0
                wheelLinkageCity?.data = mAllBeans?.get(mWheelPosition1)?.telSecondCategories
                wheelLinkageArea?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories
                wheelLinkageTown?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(mWheelPosition3)?.telFourCategories

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
                LogUtils.e("onWheelScrollStateChanged")

            }

            override fun onWheelLoopFinished(view: WheelView?) {
                LogUtils.e("onWheelLoopFinished")

            }
        })
        wheelLinkageCity?.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onWheelScrolled(view: WheelView?, offset: Int) {

            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                mWheelPosition2 = position
                mWheelPosition3 = 0
                mWheelPosition4 = 0
                wheelLinkageArea?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories
                wheelLinkageTown?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(mWheelPosition3)?.telFourCategories

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
            }

            override fun onWheelLoopFinished(view: WheelView?) {
            }
        })
        wheelLinkageArea?.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onWheelScrolled(view: WheelView?, offset: Int) {

            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                mWheelPosition3 = position
                mWheelPosition4 = 0
                wheelLinkageTown?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(mWheelPosition3)?.telFourCategories

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
            }

            override fun onWheelLoopFinished(view: WheelView?) {
            }
        })
        wheelLinkageTown?.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onWheelScrolled(view: WheelView?, offset: Int) {

            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                mWheelPosition4 = position

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
            }

            override fun onWheelLoopFinished(view: WheelView?) {
            }
        })
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()

    }


}